/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.account.service;

import com.pitop.account.api.AccountFeignApi;
import com.pitop.account.entity.Account;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.AccountException;
import com.pitop.core.exceptions.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 账户
 *
 * @author pi-top
 */
@Service("accountObjSVImpl")
@Slf4j
public class AccountSVImpl {

    @Autowired
    private AccountFeignApi accountObjFeignApi;

    /**
     * 加载一个对象Account
     *
     * @param id id* @param code 编码* @param account 账户
     * @return Account
     */
    public Account load(java.lang.Long id, java.lang.String code, java.lang.String account) {
        if (id == null && code == null && account == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return accountObjFeignApi.load(id, code, account);
    }

    /**
     * 加载一个对象Account 通过id
     *
     * @param id id
     * @return Account
     */
    public Account loadById(java.lang.Long id) {
        if (id == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return accountObjFeignApi.loadById(id);
    }

    /**
     * 加载一个对象Account 通过code
     *
     * @param code 编码
     * @return Account
     */
    public Account loadByCode(java.lang.String code) {
        if (code == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return accountObjFeignApi.loadByCode(code);
    }

    /**
     * 加载一个对象Account 通过account
     *
     * @param account 账户
     * @return Account
     */
    public Account loadByAccount(java.lang.String account) {
        if (account == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return accountObjFeignApi.loadByAccount(account);
    }


    /**
     * 删除对象Account
     *
     * @param id id* @param code 编码* @param account 账户
     */

    public void delete(java.lang.Long id, java.lang.String code, java.lang.String account) {
        if (id == null && code == null && account == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        accountObjFeignApi.delete(id, code, account);
    }


    /**
     * 查询Account分页
     *
     * @param accountObj 对象
     * @param curPage    当前页
     * @param pageSize   分页条数
     * @return List<Account>
     */
    public List<Account> list(Account accountObj, int curPage, int pageSize) {
        return accountObjFeignApi.list(accountObj, curPage, pageSize);
    }

    /**
     * 查询Account分页
     *
     * @param id       id*
     * @param code     编码*
     * @param account  账户
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<Account>
     */
    public List<Account> list(java.lang.Long id, java.lang.String code, java.lang.String account, int curPage, int pageSize) {
        if (curPage < 0) {
            curPage = 1;
        }

        if (pageSize < 0) {
            pageSize = Page.limit;
        }

        Account accountObj = new Account();
        accountObj.setId(id);
        accountObj.setCode(code);
        accountObj.setAccount(account);

        return accountObjFeignApi.list(accountObj, curPage, pageSize);
    }


    public int count(java.lang.Long id, java.lang.String code, java.lang.String account) {
        return accountObjFeignApi.count(id, code, account);
    }


    public int count(Account accountObj) {
        return accountObjFeignApi.count(accountObj);
    }


    /**
     * 保存
     * 1. 数据合法性验证
     * 2. 保存账户信息
     *
     * @param accountObj 实体
     */
    public void save(Account accountObj) {
        //1.数据合法性验证
        if (accountObj == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (accountObj.getPassword().length() < 6) {
            throw new AccountException(BaseException.BaseExceptionEnum.Password_Short);
        }
        Account account = accountObjFeignApi.loadByAccount(accountObj.getAccount());
        if (account != null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Exists);
        }
        //2. 保存账户信息
        accountObjFeignApi.build(accountObj);
    }

    /**
     * 更新
     * 1. 数据合法性验证
     * 2. 更新账户信息
     *
     * @param accountObj 实体
     */
    public void modify(Account accountObj) {
        //1. 数据合法性验证
        if (accountObj == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (accountObj.getPassword().length() < 6) {
            throw new AccountException(BaseException.BaseExceptionEnum.Password_Short);
        }
        Account account = accountObjFeignApi.loadByAccount(accountObj.getAccount());
        if (account != null && !account.getCode().equals(accountObj.getCode())) {
            throw new AccountException(BaseException.BaseExceptionEnum.Exists);
        }
        //2. 更新账户信息
        accountObjFeignApi.modify(accountObj);
    }

    /**
     * 根据账号和密码查询
     *
     * @param account  账号
     * @param password 密码
     * @return Account
     */
    public Account loadByAccountAndPwd(String account, String password) {
        return accountObjFeignApi.loadByAccountAndPwd(account, password);
    }
}
